﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using InvoiceProject.Enums;

namespace InvoiceProject.Models
{


    public class Business: BaseModel, IValidatableObject
    {
        public Business()
        {
            Address = new Address();
        }


        [Required]
        public LookupTaxCountry LookupTaxCountry { get; set; }


        [Required]
        [Display(Name = "Name *")]
        public string Name { get; set; }


        [Required]
        [Display(Name = "Phone *")]
        public string Phone { get; set; }

        [Required]
        [Display(Name = "ABN *")]
        public string ABN { get; set; }

        [Required]
        public Address Address { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [Display(Name = "Business Email address *")]
        public string Email { get; set; }

        public string UserName { get; set; }

        [Required]
        [Column(TypeName = "number")]
        [Display(Name = "Account Type *")]
        public AccountType AccountType { get; set; }


        public new IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (String.IsNullOrEmpty(this.ABN))
              yield return new ValidationResult("ABN, is null");
           
            
            long i;
            if(!long.TryParse(ABN, out i))
              yield return new ValidationResult("ABN, is not a Valid Long/Integer");

            if (this.Address == null)
              yield return new ValidationResult("Address, is null");

            if (String.IsNullOrEmpty(this.Email))
              yield return new ValidationResult("Business Email, is null");

            if (Email != null && !Email.Contains('@'))
                yield return new ValidationResult("Business Email, is not valid");
            
            if (String.IsNullOrEmpty(this.Phone))
              yield return new ValidationResult("Phone, is null");

       //Update Validation


            var address = this.Address;
            if (address != null && (this.Id !=0 && address.Id <=0))
               yield return new ValidationResult("Address, should be updated not created. (When updating a business) ");
           
        }

      

    }
}